package com.xup.luckymoney.job;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;

import com.xup.luckymoney.Config;
import com.xup.luckymoney.LuckyMoneyService;
import com.xup.luckymoney.R;

import java.util.List;

/**
 * Created by xupeng on 3/23/16.
 */
public class QQAccessibilityJob extends BaseAccessibilityJob {
    private static final String TAG = "QQAccessibilityJob";
    private static final boolean DEBUG = true;

    private static final String QQ_CHAT_INTERFACE = "com.tencent.mobileqq.activity.SplashActivity";
    private static final String QQ_PACKET_HAS_GRAB = "cooperation.qwallet.plugin.QWalletPluginProxyActivity";

    private String HONGBAO_TEXT_KEY = "";

    @Override
    public void onCreateJob(LuckyMoneyService service) {
        super.onCreateJob(service);
        service.updateQQPackageInfo();

        HONGBAO_TEXT_KEY = getContext().getResources().getString(R.string.qq_hongbao);
    }

    @Override
    public void onStopJob() {
        super.onStopJob();
    }

    @Override
    public String getTargetPackageName() {
        return Config.QQ_PACKAGENAME;
    }

    @Override
    public void onReceiveJob(AccessibilityEvent event) {
        int eventType = event.getEventType();
        // String pkgName = event.getPackageName().toString();
        switch (eventType) {
            //step 1: monitor notification change message
            case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
                List<CharSequence> texts = event.getText();
                if (!texts.isEmpty()) {
                    for (CharSequence text : texts) {
                        String content = text.toString();
                        if (DEBUG) Log.i(TAG, "text:" + content);
                        if (content.contains(HONGBAO_TEXT_KEY)) {
                            // monitor open notification bar
                            openNotify(event);
                        }
                    }
                }
                break;
            //step 2: monitor is enter hongbao interface
            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
                if (isGrabEnd()) {
                    if (DEBUG) Log.i(TAG, "not allowed to do node sniff");
                    return;
                }
                String className = event.getClassName().toString();
                if (DEBUG) Log.d(TAG, "TYPE_WINDOW_STATE_CHANGED, className = " + className);
                if (QQ_CHAT_INTERFACE.equals(className)) {
                    // QQ hongbao
                    getPacket();
                    getHongbaoEnd();
                } else if (QQ_PACKET_HAS_GRAB.equals(className)) {
                    // auto close feature (not completed)
                    // if (getConfig().autoClosePacketInterface()) {
                    // }
                }
                break;
        }
    }

    @Override
    public boolean isEnable() {
        Log.d(TAG, "getConfig().getQQEnable() = " + getConfig().getQQEnable());
        return getConfig().getQQEnable();
    }
}
